﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Socket_CS
{
    class App
    {
        IPAddress RemoteIP = IPAddress.Parse("127.0.0.1");
        int Port = 2214;
        string SendContent = "0123456789";
        int BufferSize = 1024;
        int StartReceiveServiceErrorRetryCount = 10;
        bool ServerMode = true;


        public void Run(string[] args)
        {
            InitParam(args);

            Console.WriteLine("按回车键结束.");

            using (AsyncSocket aAsyncSocket = new AsyncSocket(Port, BufferSize, StartReceiveServiceErrorRetryCount))
            {
                BindSocketLogger(aAsyncSocket);

                aAsyncSocket.OnReceived += Respond;

                aAsyncSocket.StartReceiving();
                if (!ServerMode)
                {
                    aAsyncSocket.SendTo(RemoteIP, SendContent);
                }
                Console.ReadLine();
            }

        }

        void InitParam(string[] args)
        {
            using (Params aParams = new Params())
            {
                aParams.Parse(args);

                RemoteIP = aParams.GetParamValue("-RemoteIP", RemoteIP);
                Console.WriteLine("RemoteIP: {0}", RemoteIP);
                Port = aParams.GetParamValue("-PORT", Port);
                Console.WriteLine("Port: {0}", Port);
                SendContent = aParams.GetParamValue("-SENDCONTENT", SendContent);
                Console.WriteLine("SendContent: {0}", SendContent);
                BufferSize = aParams.GetParamValue("-BUFFERSIZE", BufferSize);
                Console.WriteLine("BufferSize: {0}", BufferSize);
                ServerMode = aParams.GetParamValue("-ServerMode", ServerMode);
                Console.WriteLine("ServerMode: {0}", ServerMode);
            }
        }

        void BindSocketLogger(AsyncSocket aAsyncSocket)
        {
            aAsyncSocket.OnSent += (AsyncSocket sender, SocketState state) =>
            {
                Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Sent {1} bytes to {2}", DateTime.Now, state.MessageLength, state.RemoteEndPoint);
            };

            aAsyncSocket.OnReceivingError += (AsyncSocket sender, SocketState state, string message) =>
            {
                Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Received error for {1}, Message: {2}", DateTime.Now, state.RemoteEndPoint, message);
            };

            aAsyncSocket.OnSendError += (AsyncSocket sender, SocketState state, string message) =>
            {
                Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Send error for {1}, Message: {2}", DateTime.Now, state.RemoteEndPoint, message);
            };

        }
        void Respond(AsyncSocket aAsyncSocket, SocketState state)
        {
            Console.WriteLine("{0:yyyy-MM-dd HH:mm:ss} Received {1} bytes from {2}", DateTime.Now, state.MessageLength, state.RemoteEndPoint);
            Thread.Sleep(1000);

            string respondContent = GetRespondContent(state.StringMessage);
            aAsyncSocket.SendTo(state.RemoteEndPoint, respondContent);
        }

        string GetRespondContent(string receiveContent)
        {
            return (receiveContent.Length > 1) ? (receiveContent.Substring(1) + receiveContent[0]) : receiveContent;
        }
    }
}
